package com.luping.xiao.xiao.controller;

import com.luping.xiao.xiao.domain.AccPassengerflowHour;
import com.luping.xiao.xiao.domain.OpDispatchGpskm;
import com.luping.xiao.xiao.service.AccPassengerflowHourService;
import com.luping.xiao.xiao.service.OpDispatchGpskmService;
import com.luping.xiao.xiao.utils.controller.BaseController;
import com.luping.xiao.xiao.utils.demoin.AjaxResult;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AccPassengerflowHourController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(AccPassengerflowDetailController.class);

    @Autowired
    private AccPassengerflowHourService accPassengerflowHourService;

    @RequestMapping("accH")
    @ResponseBody
    public AjaxResult accH(String f) throws Exception {
        final int cell = 8;
        SimpleDateFormat fa = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        SimpleDateFormat faa = new SimpleDateFormat("yy-MM-dd");
        List<AccPassengerflowHour> list = new ArrayList<>();
        int lineNum=0;
        LineIterator it = null;
        try {
            it = FileUtils.lineIterator(new File(f + ".txt"));
            while (it.hasNext()) {
                lineNum++;
                //解析行数据
                String rowData = it.nextLine();
                if (rowData == null || "".equals(rowData)) {
                    continue;
                }
                log.info("lineNum="+lineNum);
                String row[] = rowData.split("\\\",");
                if (row.length != cell) {
                    row = rowData.split(",\\\"");
                }
                if (row.length != cell) {
                    row = rowData.split(",");
                }
                //id,accTime,lineId,lineName,upTimes,downTimes,direction,systime
                AccPassengerflowHour a = new AccPassengerflowHour();
                for (int m = 0; m < row.length; m++) {
                    String val = row[m];
                    if (val != null) {
                        val = val.replaceAll("\\\"", "");
                        if ("\\N".equals(val)) {
                            continue;
                        }
                        if (m == 0) {
                            a.setId(Long.decode(val));
                        } else if (m == 1) {
                            a.setAcctime(fa.parse(val));
                        } else if (m == 2) {
                            a.setLineid(Long.parseLong(val));
                        } else if (m == 3) {
                            a.setLinename((val));
                        } else if (m == 4) {
                            a.setUptimes(Long.parseLong(val));
                        } else if (m == 5) {
                            a.setDowntimes(Long.parseLong(val));
                        } else if (m == 6) {
                            a.setDirection(Integer.parseInt(val));
                        } else if (m == 7) {
                            a.setSystime(faa.parse(val));
                        }

                    }
                }
                list.add(a);
                if (list.size() > 999) {
                    saveTable(list);
                    list.clear();
                }
            }
            if(list.size()>0){
                saveTable(list);
            }
            log.info("执行成功");
        } catch (Exception e) {
            log.info("异常退出");
            e.printStackTrace();
            return error();
        } finally {
            LineIterator.closeQuietly(it);
        }
        return success();
    }


    public void saveTable(List<AccPassengerflowHour> list) {
        accPassengerflowHourService.insertList(list);

    }
}
